module <%= controller_class_name %>Helper
  
  def ajax_tree( all_branch,options = {}) 
 ret = ""
 ret +="<div id='ajax_tree' >"
  
 ret +="    <div><a onclick=$('add_#{options[:branch]}').toggle() ; href='#'>Add #{options[:branch]}</a></div>"  
 ret +="    <div id='add_#{options[:branch]}' style='display:none'> "
 ret +=     form_remote_tag :update => 'ajax_tree',:url=>{ :action => 'add_<%=@branch_name%>'}
 ret +=     text_field_tag (:<%=@branch_name%>_name,'', :class => 'text_box') 
 ret +=     submit_tag :add 
 ret +=     end_form_tag 
 ret +="    </div>"
  
 ret +="    <br /><br />"
 all_branch.each do |branch| 
 
 ret +="    <div id='#{options[:branch]}_#{options[:leaf]}_name#{branch.id}'> "
 ret +="      <div id='#{options[:branch]}#{branch.id}' class=\"branch\" >"
 ret +="        <div class=\"branch_title\" onclick=$('#{options[:branch]}_#{options[:leaf]}_block_#{branch.id}').toggle()>"  
 ret +="       #{branch.name} "
 ret +="        </div>"
 ret +="       <span class=\"add_leaf\">"
 ret +=         link_to_remote image_tag('/images/add.png'),:before => "$('add_#{options[:leaf]}#{branch.id}').toggle()"
 ret +="        </span><span class=\"delete_branch\"> "
 ret +=         link_to_remote image_tag('/images/delete.png'), :update => 'ajax_tree',:url=>{ :action => 'delete_<%=@branch_name%>' , :branch_id => "#{branch.id}"} 
 ret +="       </span> "
 ret +="      </div>"
 ret +="      <div id='#{options[:branch]}_#{options[:leaf]}_block_#{branch.id}'>"
 ret +="        <div id='add_#{options[:leaf]}#{branch.id}' style='display:none'> "
 ret +=         form_remote_tag :update => 'ajax_tree',:url=>{ :action => 'add_<%=@leaf_name%>_to_<%=@branch_name%>' , :branch_id => "#{branch.id}"}
 ret +=         text_field_tag :<%=@leaf_name%>_name
 ret +=         submit_tag :add 
 ret +=         end_form_tag
 ret +="        </div>"
                    branch.<%=@Leaf_plural%>.each do |leaf|
 ret +="            <div id='#{options[:leaf]}@#{branch.id}@#{leaf.id}'>"
 ret +="               <div class=\"leaf\">"
 ret +="                  <div class=\"leaf_title\">"
 ret +="                  #{leaf.name}"
 ret +="                  </div>"
 ret +="                 <div class=\"delete_leaf\">"
 ret +=                  link_to_remote image_tag('/images/delete.png'), :update => 'ajax_tree',:url=>{ :action => 'delete_<%=@leaf_name%>_from_<%=@branch_name%>' , :branch_id => "#{branch.id}" , :leaf_id => "#{leaf.id}"}
 ret +="                 </div>"
 ret +="               </div>" 
 ret +="            </div>"  
 ret +="           <script language='javascript' type='text/javascript'>"
 ret +="           new Draggable('#{options[:leaf]}@#{branch.id}@#{leaf.id}',{revert:true}) "
 ret +="           </script>"
                    end 
 ret +="        </div>"
 ret +="      </div>"      
 ret +="      <script language='javascript' type='text/javascript'>"
 ret +="      Droppables.add('#{options[:branch]}_#{options[:leaf]}_name#{branch.id}', {onDrop:function(element){new Ajax.Updater('ajax_tree','/<%=controller_file_name %>/receive_dragged_<%=@leaf_name%>?branch=#{branch.id}', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})"
 ret +="      </script>"
 end 
 ret +="</div>"

  
      return ret
  end
  
  
end
